﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;
using YArchitech.BIM;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;

namespace HYLightingEquipmentLay
{
	public class GarageLaySettingAuxiliary
	{
		public List<ElectricalRoomType> GetElectricRoom()
		{
			new List<ElectricalRoomType>();
			return new GarageElectricalRoomAuxiliary().LoadRoomTypeData();
		}

		public List<GarageLineSetting> GetSingleLayGarageLines()
		{
			new List<GarageLineSetting>();
			return new GarageLineSetAuxiliary().GetGarageLineInfosByLayStyle(true);
		}

		public List<GarageLineSetting> GetDoubleLayGarageLines()
		{
			new List<GarageLineSetting>();
			return new GarageLineSetAuxiliary().GetGarageLineInfosByLayStyle(false);
		}

		public GarageLineParam GetGarageLineInterfaceInfo()
		{
			new GarageLineParam();
			return new HVACUserSetting().GetGarageInterFaceInfo();
		}

		public FamilySymbol GetLayLight(Autodesk.Revit.DB.Document doc, ref string loadInfo)
		{
			FamilySymbol familySymbol = null;
			string[] lightByGarageSetting = new HVACUserSetting().GetLightByGarageSetting();
			if (string.IsNullOrEmpty(lightByGarageSetting[0]))
			{
				lightByGarageSetting[0] = "单管荧光灯_吊装";
			}
			Transaction transaction = new Transaction(doc, "LoadFamilySymbol");
			try
			{
				Family familyByName = YJKRevitTools.GetFamilyByName(lightByGarageSetting[0], doc, (Autodesk.Revit.DB.BuiltInCategory)(-2001120));
				string path = "Lamp";
				if (familyByName == null)
				{
					string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), path, lightByGarageSetting[0] + ".rfa");
					if (!File.Exists(text))
					{
						loadInfo = "数据库内未找到设定族！";
						return familySymbol;
					}
					transaction.Start();
					bool flag = doc.LoadFamily(text, out familyByName);
					transaction.Commit();
					if (flag && familyByName != null)
					{
						familySymbol = this.GetSpecSymbol(familyByName, lightByGarageSetting[1]);
					}
					else
					{
						loadInfo = "加载族失败！";
					}
				}
				else
				{
					familySymbol = this.GetSpecSymbol(familyByName, lightByGarageSetting[1]);
					if (familySymbol == null)
					{
						loadInfo = "当前项目已存在设定族，但找不到对于族类型！";
					}
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				transaction.RollBack();
			}
			return familySymbol;
		}

		private FamilySymbol GetSpecSymbol(Family family, string symbolName)
		{
			FamilySymbol result = null;
			if (family == null)
			{
				return result;
			}
			List<FamilySymbol> symbols = family.GetSymbols();
			if (symbols == null || symbols.Count < 1)
			{
				return result;
			}
			if (string.IsNullOrEmpty(symbolName) || symbolName == "空")
			{
				result = symbols.First<FamilySymbol>();
			}
			else
			{
				foreach (FamilySymbol familySymbol in symbols)
				{
					if (familySymbol != null && familySymbol.Name == symbolName)
					{
						result = familySymbol;
						break;
					}
				}
			}
			return result;
		}
	}
}
